//
//  LCCloudPlatesVC.m
//  LongChain
//
//  Created by FengZi on 2018/3/19.
//  Copyright © 2018年 FengZi. All rights reserved.
//

#import "LCCloudPlatesVC.h"
#import "LCClassificationHeader.h"
#import "LCClassificationCell.h"
#import "LCTagCell.h"
#import "LCMoneyVC.h"
#import "LCQRCodeScanVC.h"

@interface LCCloudPlatesVC ()<UICollectionViewDelegate,UICollectionViewDataSource,UICollectionViewDelegateFlowLayout>

@property (weak, nonatomic) IBOutlet UICollectionView *classificationCollection;

@property (nonatomic, strong) NSArray *datas;

@property (nonatomic, strong) NSArray *tagDatas;

@property (nonatomic, strong) NSDictionary *serviceDic;

@property (nonatomic, assign) NSInteger fileType;

@property (nonatomic, assign) NSInteger uploadFileType;

@property (nonatomic, copy) NSString *tagID;

@end

@implementation LCCloudPlatesVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.classificationCollection.hidden = YES;
    _datas = @[
                       @{
                           @"bg":@"cloudPlatesPhoneBackground",
                           @"fg":@"home_img_pic",
                           @"title":@"照片"
                           },
                       @{
                           @"bg":@"home_img_vidobg",
                           @"fg":@"home_img_vido",
                           @"title":@"视频"
                           },
                       @{
                           @"bg":@"home_img_documentbg",
                           @"fg":@"home_img_document",
                           @"title":@"文档"
                           },
                       @{
                           @"bg":@"home_img_musicbg",
                           @"fg":@"home_img_music",
                           @"title":@"音乐"
                           },
                       ];
    
    UIView *rightBarButtonBaseView = [[UIView alloc]init];
    rightBarButtonBaseView.backgroundColor = [UIColor redColor];
    UIBarButtonItem *searchItem = [[UIBarButtonItem alloc]initWithCustomView:rightBarButtonBaseView];
    
//    self.classificationCollection.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
//        [self loadFileCountData];
//    }];
    [self loadFileCountData];

    [self.classificationCollection.mj_header beginRefreshing];

    self.navigationItem.rightBarButtonItem = searchItem;
    
    
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(uploadFIleToService:) name:@"uploadFile" object:nil];
    
}

#pragma mark - - 
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if ([kind isEqualToString:UICollectionElementKindSectionHeader]) {
        LCClassificationHeader *header = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"TagHeader" forIndexPath:indexPath];
        [header lc_updateHeadWithTagCount:self.tagDatas.count];
        header.checkTags = ^{
            [self showHint:@"查看标签"];
        };
        return header;
    }else if ([kind isEqualToString:UICollectionElementKindSectionFooter]) {
        UICollectionReusableView *footer = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionFooter withReuseIdentifier:@"noDataFooter" forIndexPath:indexPath];
        return footer;
    }
    return nil;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    
    NSInteger section = indexPath.section;
    NSInteger row = indexPath.row;
    switch (section) {
        case 0:  //  总分类
        {
            if (row == 4) {
                UICollectionViewCell *allFilesCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"allFiles" forIndexPath:indexPath];
                return allFilesCell;
            }
            LCClassificationCell *classificationCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"cloudPlatesCell" forIndexPath:indexPath];
            if (_datas.count > row && self.serviceDic) {
                [classificationCell lc_updateCellWithParameter:_datas[row] andFileCount:self.serviceDic];
            }
            return classificationCell;
            
        }
            break;
        case 1: //  标签
        {
            LCTagCell *tagCell = [collectionView dequeueReusableCellWithReuseIdentifier:@"tagCell" forIndexPath:indexPath];
            if (self.tagDatas.count > row) {
                NSDictionary *tagDic = self.tagDatas[row];
                [tagCell lc_updateTagCellWithParameter:tagDic];

            }
            return tagCell;

        }
        default:
            break;
    }
    return nil;
}

- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return 2;
}
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    
    NSInteger itemCount = (section == 0) ? (_datas.count) + 1 : self.tagDatas.count;
    return itemCount;
}


- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section {
    
    CGFloat height = 0;
    switch (section) {
        case 0:  //  分类
        {
            height = 0;
        }
            break;
        case 1:  //  标签
        {
            height = 50;
        }
            break;
        default:
            break;
    }
  
    return CGSizeMake(SWidth, height);
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
    CGSize itemSize = CGSizeZero;
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;
    switch (section) {
        case 0:  //  文件类型
        {
            if (row == 4) {
                itemSize = CGSizeMake((SWidth - 20), 40);
            }else{
                itemSize = CGSizeMake((SWidth - 30)/2, (SWidth - 30)/3);

            }
        }
            break;
        case 1:  //  标签
        {
            CGFloat height = 50;
            CGFloat width = 0;
            if (self.tagDatas.count > row) {
                NSDictionary *tagDic = self.tagDatas[row];
                NSString *tagName = [NSString stringWithFormat:@"%@",tagDic[@"name"]];

                CGRect rect = [tagName boundingRectWithSize:CGSizeMake(300, 100000) options:NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:[UIFont systemFontOfSize:16.0f]} context:nil];
                
                width = (CGRectGetWidth(rect)) * 2 + 15;
                if (width < 40) {
                    width = 40;
                }
                
            }

            itemSize = CGSizeMake(width,height);

        }
            break;
            
        default:
            break;
    }
    return itemSize;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section {
    
    CGFloat footer = 0;
    switch (section) {
        case 0:
        {
            footer = 0;
        }
            break;
        case 1:
        {
            footer = (_tagDatas.count == 0) ? 200 : 0;
        }
            break;
            
        default:
            break;
    }
    return CGSizeMake(SWidth, footer);
}


- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    NSInteger row = indexPath.row;
    NSInteger section = indexPath.section;

    switch (section) {
        case 0:  //  文件
        {
            self.fileType = (row + 1);
        }
            break;
        case 1:  //  标签
        {
            NSDictionary *tagDic = self.tagDatas[row];
            NSString *tagID = [NSString stringWithFormat:@"%@",tagDic[@"tag_id"]];
            self.tagID = tagID;
            self.fileType = 6;
            
            LCLog(@"tagDic = %@",tagDic);
        }
            break;
            
        default:
            break;
    }
    [self performSegueWithIdentifier:@"showFilesList" sender:self];

}

#pragma mark - -

- (IBAction)searchFiles:(UIBarButtonItem *)sender {
    
    LCLog(@"搜索");
    
    [self performSegueWithIdentifier:@"showSearch" sender:self];
}
- (IBAction)money:(id)sender {
    
    
}

- (void)uploadFIleToService:(NSNotification *)noti {
    LCLog(@"userInfo = %@",noti.userInfo);
    
    NSArray *devices = noti.userInfo[@"devices"];
    if (devices.count > 0) {
        self.uploadFileType = 0;
        NSString *type = [NSString stringWithFormat:@"%@",noti.userInfo[@"type"]];
        self.uploadFileType = [type integerValue];
        //    NSInteger uploadType = [type integerValue];
        [self performSegueWithIdentifier:@"showUploadFilesList" sender:self];

    }else{
        LCQRCodeScanVC *qrCodeScanVC = [[LCQRCodeScanVC alloc]init];
        qrCodeScanVC.hidesBottomBarWhenPushed = YES;
        [self.navigationController pushViewController:qrCodeScanVC animated:YES];
    }
}



//  数据请求
- (void)loadFileCountData {

    WS(weakSelf);
    [[LCNetwork shareInstance]lc_getNetworkWithURL:@"/api/v0/nas/overview" responseSuccess:^(id resultCode, id resultParameter) {
        
        [weakSelf hideHud];
        LCLog(@"请求文件数量 = %@",resultParameter);
        NSDictionary *data = resultParameter[@"data"];
        NSDictionary *count = data[@"count"];
        weakSelf.serviceDic = count;
        NSString *audioCount = [NSString stringWithFormat:@"%@",count[@"audio"]];
        NSString *imageCount = [NSString stringWithFormat:@"%@",count[@"image"]];
        NSString *textCount = [NSString stringWithFormat:@"%@",count[@"text"]];
        NSString *totalCount = [NSString stringWithFormat:@"%@",count[@"total"]];
        NSString *videoCount = [NSString stringWithFormat:@"%@",count[@"video"]];
        LCLog(@"audioCount = %@ imageCount = %@ textCount = %@ totalCount = %@ videoCount = %@",audioCount,imageCount,textCount,totalCount,videoCount);
        weakSelf.classificationCollection.hidden = NO;
        [weakSelf.classificationCollection.mj_header endRefreshing];
        [weakSelf.classificationCollection reloadData];
        
        NSArray *tag = data[@"tag"];
        self.tagDatas = tag;
        NSLog(@"tag = %@",tag);
        
    } responseFail:^(id resultCode, NSError *error) {
        
        [weakSelf.classificationCollection.mj_header endRefreshing];
        [weakSelf hideHud];
        [weakSelf showHint:@"网络异常"];

    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
    
    if ([segue.identifier isEqualToString:@"showFilesList"]) {
        id fileListVC = segue.destinationViewController;
        [fileListVC setValue:@(self.fileType) forKey:@"fileType"];
        [fileListVC setValue:self.tagID forKey:@"tagID"];
        
    }else if ([segue.identifier isEqualToString:@"showUploadFilesList"]){
        
        id uploadFileVC = segue.destinationViewController;
        [uploadFileVC setValue:@(self.uploadFileType) forKey:@"uploadFileType"];
    }
}


@end
